package com.ruoyi.web.controller.mall;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.Category;
import com.ruoyi.system.domain.Dict;
import com.ruoyi.system.domain.Goods;
import com.ruoyi.system.domain.GoodsComment;
import com.ruoyi.system.domain.vo.GoodsAddVo;
import com.ruoyi.system.domain.vo.GoodsDetailVo;
import com.ruoyi.system.domain.vo.GoodsQueryVo;
import com.ruoyi.system.domain.vo.GoodsSpecVo;
import com.ruoyi.system.mapper.*;
import com.ruoyi.web.controller.tool.ShortLinkUtil;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/goods")
public class GoodsController extends BaseController {

    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private GoodsSpecMapper goodsSpecMapper;
    @Resource
    private DictMapper dictMapper;
    @Resource
    private GoodsCommentMapper goodsCommentMapper;

    public static List<String> picList = null;

    @PostConstruct
    public void gen(){
        picList = goodsMapper.picList();
    }


    @PostMapping("/add")
    public AjaxResult add(@RequestBody GoodsAddVo vo) {
        Goods goods = new Goods();
        BeanUtils.copyBeanProp(goods,vo);
        goods.setCreateTime(new Date());
        goods.setLabel("1,2,3");
        goodsMapper.insert(goods);
        insertComment(goods.getId(),vo.getCommentList());
        return success();
    }

    private void insertComment(Long goodsId,List<GoodsComment> remarks){
        if (CollectionUtils.isEmpty(remarks)){
            return;
        }
        List<GoodsComment> list = new ArrayList<>();
        Random random = new Random();
        for (GoodsComment goodsComment : remarks) {
            int anInt = random.nextInt(picList.size());
            String name = ShortLinkUtil.generateRandomString(7);
            String nameBuild = name.charAt(0)+"***"+name.charAt(name.length()-1);
            goodsComment.setRemark(goodsComment.getRemark());
            goodsComment.setName(nameBuild);
            goodsComment.setAvatar(picList.get(anInt));
            goodsComment.setCreateTime(new Date());
            goodsComment.setScore(5);
            list.add(goodsComment);
        }
        goodsCommentMapper.insertBatch(goodsId,list);
    }

    @PostMapping("/update")
    public AjaxResult update(@RequestBody GoodsAddVo vo) {
        Goods goods = new Goods();
        BeanUtils.copyBeanProp(goods,vo);
        goodsMapper.updateById(goods);
        goodsSpecMapper.deleteByGoodsId(vo.getId());
        if (!CollectionUtils.isEmpty(vo.getSpecList())){
            goodsSpecMapper.insertBatch(vo.getSpecList(),vo.getId());
        }
        goodsCommentMapper.deleteByGoodsId(vo.getId());
        insertComment(goods.getId(),vo.getCommentList());
        return success();
    }


    @GetMapping("/queryById/{id}")
    public AjaxResult queryById(@PathVariable Long id) {
        GoodsDetailVo vo = goodsMapper.selectInfoById(id);
        List<Category> categories = categoryMapper.selectList(new QueryWrapper<>());
        List<GoodsSpecVo> goodsSpecVos = goodsSpecMapper.queryGoodsSpec(id);
        List<Dict> dictList = dictMapper.selectByType(1);
        List<GoodsComment> commentList = goodsCommentMapper.selectByGoodsId(id);
        vo.setDictList(dictList);
        vo.setSpecList(goodsSpecVos);
        vo.setCategoryList(categories);
        vo.setCommentList(commentList);
        return success(vo);
    }

    @GetMapping("/queryCategoryList")
    public AjaxResult queryCategoryList() {
        List<Category> list = categoryMapper.selectList(new QueryWrapper<>());
        return success(list);
    }

    @GetMapping("/queryDictList")
    public AjaxResult queryDictList() {
        List<Dict> list = dictMapper.selectByType(1);
        return success(list);
    }

    @GetMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        Goods goods = new Goods();
        goods.setId(id);
        goods.setStatus(2);
        goodsMapper.updateById(goods);
        return success();
    }


    @GetMapping("/queryList")
    public TableDataInfo queryList(GoodsQueryVo vo) {
        startPage();
        List<GoodsDetailVo> list = goodsMapper.queryList(vo);
        return getDataTable(list);
    }
}
